﻿
package com.android.finaltracer;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.android.finaltracer.model.BindInterface;
import com.android.finaltracer.model.FTDataProvider.RelationColumns;

public class RelationNumberCursorAdapter extends CursorAdapter {
    private final LayoutInflater mInflate;
    protected Context mContext;
    private BindInterface bindInterface;
    private ArrayList<String> phones;

    public RelationNumberCursorAdapter(Context context, Cursor c,
            BindInterface bindInterface) {
        super(context, c);
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflate = LayoutInflater.from(context);
        this.bindInterface = bindInterface;
        phones = new ArrayList<String>();
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        ViewHolder holder = (ViewHolder) view.getTag();
        final String phoneNumber = cursor.getString(cursor
                .getColumnIndex(RelationColumns.NUMBER));
        final int weightType = cursor.getInt(cursor
                .getColumnIndex(RelationColumns.WEIGHT_TYPE));
        final int keyID = cursor.getInt(cursor
                .getColumnIndex(RelationColumns._ID));
        if (phoneNumber != null && phoneNumber.length() > 0) {
            if (phones.contains(phoneNumber)) {
                view.setBackgroundColor(Color.rgb(28, 135, 177));
            } else {
                view.setBackgroundColor(Color.WHITE);
            }
            holder.phone_number_tv.setText(phoneNumber);
            if (weightType == RelationColumns.WEIGHT_TYPE_UNBIND) {
                holder.bind_status_tv.setText(mContext.getResources()
                        .getString(R.string.bind_status_unbind));
                holder.bind_btn.setText("绑定");
                holder.bind_btn
                        .setOnClickListener(new BindListener(phoneNumber));
                holder.bind_btn.setVisibility(View.VISIBLE);
            } else if (weightType == RelationColumns.WEIGHT_TYPE_WAIT_BIND) {
                holder.bind_status_tv.setText(mContext.getResources()
                        .getString(R.string.bind_status_wait_bind));
                holder.bind_btn.setVisibility(View.GONE);
            } else if (weightType == RelationColumns.WEIGHT_TYPE_BINDED) {
                holder.bind_status_tv.setText(mContext.getResources()
                        .getString(R.string.bind_status_binded));
                holder.bind_btn.setVisibility(View.GONE);
            } else {
                holder.bind_status_tv.setText(mContext.getResources()
                        .getString(R.string.bind_status_unknown));
                holder.bind_btn.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = newView(mContext, parent, R.layout.relation_number_item);
        return v;
    }

    protected View newView(Context context, ViewGroup parent, int rid) {
        final View view = mInflate.inflate(rid, parent, false);
        ViewHolder holder = new ViewHolder();
        holder.phone_number_tv = (TextView) view
                .findViewById(R.id.phone_number_tv);
        holder.bind_status_tv = (TextView) view
                .findViewById(R.id.bind_status_tv);
        holder.bind_btn = (Button) view.findViewById(R.id.bind_btn);
        view.setTag(holder);
        return view;
    }

    private class BindListener implements OnClickListener {
        private String phoneNumber;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            bindInterface.bindReq(phoneNumber);
        }

        public BindListener(String phoneNumber) {
            super();
            this.phoneNumber = phoneNumber;
        }

    }

    public final class ViewHolder {
        public TextView phone_number_tv;
        public TextView bind_status_tv;
        public Button bind_btn;
        // public ImageView do_btn;
    }

    // add hw begin 5.23
    public void togglePhone(String phone) {
        if (phones.contains(phone)) {
            phones.remove(phone);
        } else {
            phones.add(phone);
        }
        notifyDataSetChanged();
    }

    public ArrayList<String> getPhones() {
        return phones;
    }

    public void clearPhones() {
        phones.clear();
        notifyDataSetChanged();
    }
    // add hw end 5.23
}
